import router, { constantRoutes } from '@/router'
import { getRouters, menuList } from '@/api/menu'
import Layout from '@/layout/index'
const permission = {
  state: {
    routes: [],
    addRoutes: [],
    defaultRoutes: [],
    topbarRouters: [],
    sidebarRouters: [],
    permissionList: [],
    menuData: [
      {
        "menuId": "1",
        "menuName": "信息管理",
        "parentId": "0",
        "orderNum": 0,
        "path": "/information",
        "component": "",
        "query": "",
        "isFrame": 1,
        "menuType": "M",
        "visible": 0,
        "status": 0,
        "perms": null,
        "icon": "infoicon",
        "createTime": "2023-07-21T15:27:45",
        "updateTime": "2023-07-22T12:27:09",
        "remark": ""
      },
      {
        "menuId": "2",
        "menuName": "电站管理",
        "parentId": "1",
        "orderNum": 0,
        "path": "/powerstation",
        "component": "/information/powerstation",
        "query": "",
        "isFrame": 1,
        "menuType": "C",
        "visible": 0,
        "status": 0,
        "perms": null,
        "icon": "powerstation",
        "createTime": "2023-07-21T15:28:07",
        "updateTime": "2023-07-22T12:27:14",
        "remark": ""
      },
      {
        "menuId": "3",
        "menuName": "区域管理",
        "parentId": "1",
        "orderNum": 0,
        "path": "/district",
        "component": "/information/district",
        "query": "",
        "isFrame": 1,
        "menuType": "C",
        "visible": 0,
        "status": 0,
        "perms": null,
        "icon": "district",
        "createTime": "2023-07-21T15:28:07",
        "updateTime": "2023-07-22T12:27:14",
        "remark": ""
      },
      {
        "menuId": "3",
        "menuName": "设备管理",
        "parentId": "1",
        "orderNum": 0,
        "path": "/equipment",
        "component": "/information/equipment",
        "query": "",
        "isFrame": 1,
        "menuType": "C",
        "visible": 0,
        "status": 0,
        "perms": null,
        "icon": "equipment",
        "createTime": "2023-07-21T15:28:07",
        "updateTime": "2023-07-22T12:27:14",
        "remark": ""
      },
      {
        "menuId": "10",
        "menuName": "日志管理",
        "parentId": "0",
        "orderNum": 0,
        "path": "/journal",
        "component": "",
        "query": "",
        "isFrame": 1,
        "menuType": "",
        "visible": 0,
        "status": 0,
        "perms": null,
        "icon": "journal",
        "createTime": "2023-07-21T15:10:57",
        "updateTime": "2023-07-21T15:22:02",
        "remark": ""
      },
      {
        "menuId": "11",
        "menuName": "操作日志",
        "parentId": "10",
        "orderNum": 0,
        "path": "/operation",
        "component": "/journal/operation",
        "query": "",
        "isFrame": 1,
        "menuType": "",
        "visible": 0,
        "status": 0,
        "perms": null,
        "icon": "journalds",
        "createTime": "2023-07-21T15:11:25",
        "updateTime": "2023-07-22T12:22:05",
        "remark": ""
      },
      {
        "menuId": "12",
        "menuName": "报警日志",
        "parentId": "10",
        "orderNum": 0,
        "path": "/caution",
        "component": "/journal/caution",
        "query": "",
        "isFrame": 1,
        "menuType": "",
        "visible": 0,
        "status": 0,
        "perms": null,
        "icon": "journalds",
        "createTime": "2023-07-21T15:17:16",
        "updateTime": "2023-07-22T12:22:51",
        "remark": ""
      },
      {
        "menuId": "13",
        "menuName": "常规日志",
        "parentId": "10",
        "orderNum": 0,
        "path": "/convention",
        "component": "/journal/convention",
        "query": "",
        "isFrame": 1,
        "menuType": "",
        "visible": 0,
        "status": 0,
        "perms": null,
        "icon": "journalds",
        "createTime": "2023-07-21T15:17:16",
        "updateTime": "2023-07-22T12:22:51",
        "remark": ""
      },
      {
        "menuId": "14",
        "menuName": "定时日志",
        "parentId": "10",
        "orderNum": 0,
        "path": "/timing",
        "component": "/journal/timing",
        "query": "",
        "isFrame": 1,
        "menuType": "",
        "visible": 0,
        "status": 0,
        "perms": null,
        "icon": "journalds",
        "createTime": "2023-07-21T15:17:16",
        "updateTime": "2023-07-22T12:22:51",
        "remark": ""
      },
      {
        "menuId": "20",
        "menuName": "系统管理",
        "parentId": "0",
        "orderNum": 0,
        "path": "/system",
        "component": "",
        "query": "",
        "isFrame": 1,
        "menuType": "",
        "visible": 0,
        "status": 0,
        "perms": null,
        "icon": "system",
        "createTime": "2023-07-21T15:10:57",
        "updateTime": "2023-07-21T15:22:02",
        "remark": ""
      },
      {
        "menuId": "21",
        "menuName": "角色管理",
        "parentId": "20",
        "orderNum": 0,
        "path": "/roleicon",
        "component": "/system/roleicon",
        "query": "",
        "isFrame": 1,
        "menuType": "",
        "visible": 0,
        "status": 0,
        "perms": null,
        "icon": "roleicon",
        "createTime": "2023-07-21T15:11:25",
        "updateTime": "2023-07-22T12:22:05",
        "remark": ""
      },
      {
        "menuId": "22",
        "menuName": "权限管理",
        "parentId": "20",
        "orderNum": 0,
        "path": "/jurisdiction",
        "component": "/system/jurisdiction",
        "query": "",
        "isFrame": 1,
        "menuType": "",
        "visible": 0,
        "status": 0,
        "perms": null,
        "icon": "jurisdiction",
        "createTime": "2023-07-21T15:17:16",
        "updateTime": "2023-07-22T12:22:51",
        "remark": ""
      },
      {
        "menuId": "23",
        "menuName": "账号管理",
        "parentId": "20",
        "orderNum": 0,
        "path": "/accountuser",
        "component": "/system/accountuser",
        "query": "",
        "isFrame": 1,
        "menuType": "",
        "visible": 0,
        "status": 0,
        "perms": null,
        "icon": "accountuser",
        "createTime": "2023-07-21T15:17:16",
        "updateTime": "2023-07-22T12:22:51",
        "remark": ""
      },
      {
        "menuId": "24",
        "menuName": "企业管理",
        "parentId": "20",
        "orderNum": 0,
        "path": "/enterprise",
        "component": "/system/enterprise",
        "query": "",
        "isFrame": 1,
        "menuType": "",
        "visible": 0,
        "status": 0,
        "perms": null,
        "icon": "enterprise",
        "createTime": "2023-07-21T15:17:16",
        "updateTime": "2023-07-22T12:22:51",
        "remark": ""
      },
      {
        "menuId": "25",
        "menuName": "菜单管理",
        "parentId": "20",
        "orderNum": 0,
        "path": "/adminmenu",
        "component": "/system/adminmenu",
        "query": "",
        "isFrame": 1,
        "menuType": "",
        "visible": 0,
        "status": 0,
        "perms": null,
        "icon": "adminmenu",
        "createTime": "2023-07-21T15:17:16",
        "updateTime": "2023-07-22T12:22:51",
        "remark": ""
      },
    ],
  },
  mutations: {
    SET_NAME: (state, name) => {
      localStorage.setItem('name', JSON.stringify(name))
      state.name = name
    },
    SET_ROUTES: (state, routes) => {
      state.addRoutes = routes
      state.routes = constantRoutes.concat(routes)
    },
    SET_DEFAULT_ROUTES: (state, routes) => {
      state.defaultRoutes = constantRoutes.concat(routes)
    },
    SET_TOPBAR_ROUTES: (state, routes) => {
      state.topbarRouters = routes
    },
    SET_SIDEBAR_ROUTERS: (state, routes) => {
      state.sidebarRouters = routes
    },
  },
  actions: {
    // 生成路由
    GenerateRoutes({ commit }) {
      let that = this
      return new Promise((resolve, reject) => {
        getRouters().then((res) => {
          if (res.code == 200) {
            const asyncRoutes = menuListEdit(res.data)
            router.addRoutes(asyncRoutes);
            commit('SET_ROUTES', asyncRoutes)
            commit('SET_SIDEBAR_ROUTERS', constantRoutes.concat(asyncRoutes))
            commit('SET_DEFAULT_ROUTES', asyncRoutes)
            commit('SET_TOPBAR_ROUTES', asyncRoutes)
            resolve(asyncRoutes)
          }
        })

        // 向后端请求路由数据
        // getRouters().then(res => {
        //   if (res.data) {
        //     let names = {}
        //     names.userName = res.data.userInfo.account
        //     names.phoneNumber = res.data.userInfo.phoneNumber
        //     names.name = res.data.userInfo.username
        //     names.userAvatarUrl = res.data.userInfo.userAvatarUrl
        //     commit('SET_NAME', names)
        //     console.log(permission.state.menuData,'惨淡');
        //     // const asyncRoutes = listRouter(res.data.userInfo.menuList)
        //     const asyncRoutes = listRouter(permission.state.menuData)
        //     router.addRoutes(asyncRoutes);
        //     commit('SET_ROUTES', asyncRoutes)
        //     commit('SET_SIDEBAR_ROUTERS', constantRoutes.concat(asyncRoutes))
        //     commit('SET_DEFAULT_ROUTES', asyncRoutes)
        //     commit('SET_TOPBAR_ROUTES', asyncRoutes)
        //     resolve(asyncRoutes)
        //   } else {
        //     reject(res)
        //   }
        // })
      })
    }
  }
}
//菜单处理
function menuListEdit(RouterData) {
  console.log(RouterData, '121321');

  let newRouter = []
  let newarr = []
  let full = []
  RouterData.map(item => {
    if (item.pid == '0') {
      item.children = []
      let Fdata = {
        path: item.url,
        component: Layout,
        children: item.children,
        redirect: item.url,
        meta: {
          title: item.name,
          icon: item.icon
        }
      }
      full.push(item)
      newRouter.push(Fdata)
    }
    if (item.child) {
      item.child.map(v => {
        let data = {
          name: v.url.substring(0),
          path: v.url,
          component: loadView(v.controller),
          meta: {
            title: v.name,
            icon: v.icon
          },
        }
        item.children.push(data)

      })
    }
  })
  console.log(newRouter,'结果');
  return newRouter
}
function listRouter(RouterData) {
  let newRouter = []
  let newarr = []
  let full = []
  RouterData.map(item => {
    if (item.parentId == '0') {
      item.children = []
      let Fdata = {
        path: item.path,
        component: Layout,
        children: item.children,
        redirect: item.path,
        meta: {
          title: item.menuName,
          icon: item.icon
        }
      }
      full.push(item)
      newRouter.push(Fdata)
    } else {
      newarr.push(item)
    }
  })
  full.map(item => {
    newarr.map(v => {
      let indexAV = v.path.indexOf('/')
      let indexBV = v.path.indexOf('/', indexAV + 1)
      if (item.menuId == v.parentId) {
        let data = {
          name: indexBV == '-1' ? v.path.slice(indexAV + 1) : v.path.slice(indexBV + 1),
          path: v.query ? v.path + v.query : v.path,
          component: loadView(v.component),
          meta: {
            title: v.menuName,
            icon: v.icon
          },
          hidden: v.visible == 1 ? true : false
        }
        item.children.push(data)
      }
    })
  })
  console.log(newRouter, '菜单');
  return newRouter
}
//生成动态路由component路径
export const loadView = (view) => {
  if (process.env.NODE_ENV === 'development') {
    return (resolve) => require([`@/views${view}`], resolve)
  } else {
    // 使用 import 实现生产环境的路由懒加载
    return () => import(`@/views${view}`)
  }
}

export default permission
